package com.DR;
import lombok.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.util.*;
import java.util.List;

@Setter
@Getter
public class Listener extends MouseAdapter implements ActionListener{
    public int X0= 135, Y0 = 100;
    public int SIZE = 20,LINE = 30;
    public Graphics g;  //画笔对象
    public int x1,y1,x2,y2;
    public int[][] numberArr = new int[LINE][LINE];
    @Setter
    public UI ui = new UI();
    Map<Integer, List<int[][]>> map = new HashMap<>();
    String type = " ";
    String number = "";
    int key = 5;
    public Listener(Graphics g){
        this.g = g;
        Graphics2D g2d = (Graphics2D) g;
        g2d.setStroke(new BasicStroke(3));
    }

    public void mousePressed(MouseEvent e) {
        x1 = e.getX();
        y1 = e.getY();
    }

    public void mouseDragged(MouseEvent e){
        x2 = e.getX();
        y2 = e.getY();
        int x = (x2 - X0)/SIZE;
        int y = (y2 - Y0)/SIZE;
        numberArr[x][y] = 1;

        g.drawLine(x1,y1,x2,y2);
        x1 = x2;
        y1 = y2;
    }

    public void mouseReleased(MouseEvent e) {
        printArr(numberArr);
    }

    public void printArr(int[][] arr){
        for (int i = 0; i < LINE; i++) {
            for (int j = 0; j < LINE; j++) {
                System.out.print(arr[j][i] + " ");
            }
            System.out.println();
        }
        System.out.println();
    }

    @SneakyThrows
    @Override
    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        if (actionCommand.equals("comboBoxChanged")) {
            JComboBox<String> comboBox = (JComboBox<String>) e.getSource();
            String selectedNumber = (String) comboBox.getSelectedItem();
            number = selectedNumber;
            System.out.println(number);
        }
        else {
            type = actionCommand;
            System.out.println(type);
        }
        action(type, number);
    }

    public void action(String t, String n) throws IOException {
        if (t.equals("刷新")) {
            for (int i = 0; i < LINE; i++) {
                for (int j = 0; j < LINE; j++) {
                    numberArr[i][j] = 0;
                }
            }
            ui.paint(ui.getGraphics());
            return;
        }
        else if (t.equals("录入")) {
            System.out.println("MyNum//" + n + ".txt");
            writeNumber(new File("MyNum//" + n + ".txt"), numberArr);
        }
        else if (t.equals("识别")) {
            map = inArr();
            int result = identificationNumber(map, numberArr);

        }

    }

    public void writeNumber(File file, int[][] arr) throws IOException {
        DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(file, true));
        for (int i = 0; i < LINE; i++) {
            for (int j = 0; j < LINE; j++) {
                dataOutputStream.write(arr[j][i]);
            }
        }
        dataOutputStream.close();
        for (int i = 0; i < LINE; i++) {
            for (int j = 0; j < LINE; j++) {
                numberArr[j][i] = 0;
            }
        }
    }

    public Map<Integer, List<int[][]>> inArr() throws IOException {
        Map<Integer, List<int[][]>> result = new HashMap<>();
        for (int i = 0; i <= 9; i++) {
            File file = new File("MyNum//" + i + ".txt");
            if (!file.exists()) {
                System.out.println("MyNum//" + i + ".txt" + "no exist");
                continue;
            }
            System.out.println("MyNum//" + i + ".txt");
            //读取每个数字的样例
            FileInputStream fileInputStream = new FileInputStream(file);
            DataInputStream dataInputStream = new DataInputStream(fileInputStream);
            List<int[][]> arrayList = new ArrayList<>();
            while (dataInputStream.available() > 0) {
                int[][] arrTemp = new int[LINE][LINE];
                for (int j = 0; j < LINE; j++) {
                    for (int k = 0; k < LINE; k++) {
                        arrTemp[k][j] = dataInputStream.readInt();
                    }
                }
                arrayList.add(arrTemp);
            }
            dataInputStream.close();
            result.put(i, arrayList);
        }
        return result;
    }

    public int identificationNumber(Map<Integer, List<int[][]>> map, int[][] numberArr) {
        Map<Integer,Integer> temp = new HashMap<>();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i <= 9; i++) {
            List<int[][]> numList = map.get(i);
            if (numList == null || numList.isEmpty()) {
                continue;
            }
            int res =  everyNumIdent(numList,i );
            if (res < min) {
                min = res;
            }
        }

        return min;
    }

    public int everyNumIdent(List<int[][]> nums,int num) {
        List<Integer> maxNum = new ArrayList<>();
        for (int[][] arr : nums) {
            int diff = 0;
            for (int i = 0; i < LINE; i++) {
                for (int j = 0; j < LINE; j++) {
                    if (arr[j][i] - numberArr[j][i] == -1)
                        diff++;
                    if (arr[j][i] - numberArr[j][i] == 1)
                        diff++;
                }
            }
            System.out.println(num);
            maxNum.add(diff);
        }
        Collections.sort(maxNum);
        if (maxNum.size() < key) {
            int res = 0;
            for (int i : maxNum) {
                res += i;
                System.out.println(num + " : " + i + "小于key");
            }
            return res;
        }
        else {
            List<Integer> smallestFive = maxNum.subList(0, key);
            int res = 0;
            for (int i : smallestFive) {
                res += i;
                System.out.println(num + " : " + i + "大于key");
            }
            return res;
        }
    }
}
